﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Utility;
using Portal.Cms.Model;
using Portal.Cms.Bussiness;
using Portal.Core;

namespace Portal.Cms.Render
{
    public class StatiticsRenderBll
    {
        public static AjaxOut Draw(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                bool per = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcStatitics, "8c604984-e627-4f2b-80f7-26f94a39a4c5", WebSessionUtility.GetCurrentLoginUser().UserId, true);

                if (!per)
                {
                    throw new Exception("Không có quyền truy cập chức năng quản trị. Để vào chức năng này xin vui lòng đăng nhập bằng tài khoản admin");
                }


                DateTime dFromDate = new System.DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, 1);
                DateTime dToDate = System.DateTime.Now;
                string SelectField =
                    "<select id=drpSelectDateField class=drpSelect>\r\n" +
                    "   <option value=\"ReleaseDate\">Ngày ban hành</option>\r\n" +
                    "   <option value=\"EffectedDate\">Ngày hiệu lực</option>\r\n" +
                    "</select>\r\n";

                //string BackUrl = (string)WebEnvironments.Request(SiteId, "BackUrl");
                //if (string.IsNullOrEmpty(BackUrl))
                //{
                //    BackUrl = ORenderInfo.BackUrl;
                //    if (string.IsNullOrEmpty(BackUrl))
                //    {
                //        BackUrl = "folder.aspx";
                //    }
                //}
                //else
                //{
                //    BackUrl = "../../folder.aspx?folder=" + BackUrl;
                //}
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string HttpRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        FromDate = document.getElementById('txtFromDate').value;\r\n" +
                   "        ToDate = document.getElementById('txtToDate').value;\r\n" +
                   "        DateField= document.getElementById('drpSelectDateField').value;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.StatiticsRenderBll.ServerSideReading(SiteId,DateField, FromDate, ToDate).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListDocumentReleaseStatitics').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +


                   "    function ViewDetail(ViewType,Id)\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('txtViewType').value=ViewType;\r\n" +
                   "        document.getElementById('txtFilterId').value=Id;\r\n" +
                   "        ReadingDetail(0);\r\n" +
                   "    }\r\n" +

                   "    function ReadingDetail(PageIndex)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        ViewType=document.getElementById('txtViewType').value;\r\n" +
                   "        Id=document.getElementById('txtFilterId').value;\r\n" +
                   "        FromDate = document.getElementById('txtFromDate').value;\r\n" +
                   "        ToDate = document.getElementById('txtToDate').value;\r\n" +
                   "        DateField= document.getElementById('drpSelectDateField').value;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.StatiticsRenderBll.ServerSideReadingDocumentDetail(SiteId,DateField,FromDate,ToDate, ViewType,Id).value;\r\n" +
                   "        document.getElementById('divListViewDetail').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                   "</script>\r\n" +
                   "<input id=txtCheckedIds type=hidden value=\"\">\r\n" +
                    //"<div id=divActionForm class=CssFormSaveUpdate></div>\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientReading(0);\"><img class=image src=\"" + HttpRoot + "/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:history.back(-1);\"><img class=image src=\"" + HttpRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <h3>Thống kê văn bản</h3>\r\n" +
                   "  <input id=txtViewType type=hidden value=\"\">\r\n" +
                   "  <input id=txtFilterId type=hidden value=\"\">\r\n" +
                   "  <div class=CssFilter>Loại " + SelectField + " - Từ ngày: " + LookUpUtilities.GenerateLookUpDate("txtFromDate", dFromDate) + " đến ngày " + LookUpUtilities.GenerateLookUpDate("txtToDate", dToDate) + "<input type=button class=button value=\"Lọc dữ liệu\" onclick=\"javascript:ClientReading();\"></div>\r\n" +
                   "  <div class=divMainForm>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>\r\n" +
                   "            <div id=\"divListDocumentReleaseStatitics\" class=tabcontent>\r\n" +
                                    ServerSideReading(SiteId, "ReleaseDate", dFromDate.ToString("dd/MM/yyyy"), dToDate.ToString("dd/MM/yyyy")).HtmlContent +
                    //ServerSideReading(SiteId, "ReleaseDate", dFromDate.ToString("yyyy/MM/dd"), dToDate.ToString("yyyy/MM/dd")).HtmlContent +
                   "             </div>\r\n" +
                   "        </td>\r\n" +
                   "        <td class=Col2>\r\n" +
                   "            <div id=divListViewDetail class=tabcontent><span class=CssTip>Kích vào danh mục bên trái để xem chi tiết danh sách văn bản</span></div>\r\n" +
                   "        </td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(
            string SiteId,
            string DateField,
            string FromDate,
            string ToDate)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (FunctionUtilities.checkVnDate(FromDate) == false)
                {
                    throw new Exception("Ngày không hợp lệ!");
                }
                if (FunctionUtilities.checkVnDate(ToDate) == false)
                {
                    throw new Exception("Ngày không hợp lệ!");
                }
                DateTime dFromDate = FunctionUtilities.VNDateToDate(FromDate);
                DateTime dToDate = FunctionUtilities.VNDateToDate(ToDate);
                ArrayList DocumentStatitics = DocStatiticsProcessBll.ReadingDocumentStatitics(SiteId, DateField, dFromDate, dToDate);
                if (DocumentStatitics.Count == 0)
                {
                    OAjaxOut.HtmlContent = "<div class=CssNoData>Không có dữ liệu thống kê từ ngày " + FromDate + " đến ngày " + ToDate + "</div>\r\n";
                    return OAjaxOut;
                }

                string[] Heading = new string[] { "Loại văn bản", "Lĩnh vực", "Người ký", "Cơ quan ban hành", "Thời gian" };
                string[] Fields = new string[] { "DocType", "DocField", "DocSinger", "DocReleaseFrom", "DocYearRange" };
                string Html = "";
                for (int iIndex = 0; iIndex < Heading.Length; iIndex++)
                {
                    DocStatiticsCls[] DocStatitics = (DocStatiticsCls[])DocumentStatitics[iIndex];
                    Html +=
                       "<ul class=CssUlItem>\r\n" +
                       "    <h3>" + Heading[iIndex] + "</h3>\r\n";
                    for (int iIndexRow = 0; iIndexRow < DocStatitics.Length; iIndexRow++)
                    {
                        Html +=
                           "    <li><a href=\"javascript:ViewDetail('" + Fields[iIndex] + "','" + DocStatitics[iIndexRow].Id + "');\">" + DocStatitics[iIndexRow].Subject + "</a> (" + DocStatitics[iIndexRow].Total.ToString("#,##0") + ")</li>\r\n";
                    }
                    Html += "</ul>\r\n";
                }

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingDocumentDetail(
            string SiteId,
            string DateField,
            string FromDate,
            string ToDate,
            string Field,
            string Id,
            int PageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DateTime dFromDate = FunctionUtilities.VNDateToDate(FromDate);
                DateTime dToDate = FunctionUtilities.VNDateToDate(ToDate);
                CmsDataCls[] CmsDatas = DocStatiticsProcessBll.ReadingCmsData(SiteId, DateField, dFromDate, dToDate, Field, Id);
                ReturnPaging OReturnPaging =
                    WebPaging.GetPaging(CmsDatas.Length, PageIndex, 20, 10, "ReadingDetail");
                if (CmsDatas.Length == 0)
                {
                    OAjaxOut.HtmlContent += "<div class=CssNoData>Không có văn bản nào</div>\r\n";
                    return OAjaxOut;
                }
                string Html =
                    OReturnPaging.PagingText +
                    "<table>\r\n" +
                    "   <tr>\r\n" +
                    "       <th class=thIndex>#</th>\r\n" +
                    "       <th class=thCode>Số / Tiêu đề</th>\r\n" +
                    "       <th class=thCode>Ngày ban hành</th>\r\n" +
                    "       <th class=thCode>Ngày hiệu lực</th>\r\n" +
                    "   </tr>\r\n";
                for (int iIndex = 0; iIndex < CmsDatas.Length; iIndex++)
                {
                    string Url = Utility.GetUrlDetails(CmsDatas[iIndex].Subject, CmsDatas[iIndex].DataId, SiteId, string.Empty);
                    Html +=
                        "<tr>\r\n" +
                        "   <td>" + (iIndex + 1) + "</td>\r\n" +
                        //"   <td><a href=\"PreviewCmsData.aspx?gid=" + CmsDatas[iIndex].frkGroupDataId + "&CmsDataId=" + CmsDatas[iIndex].DataId + "&Back=StatiticsReleaseDocument.aspx\" target='_blank'>" + CmsDatas[iIndex].DataAlias + "</a><br>" + CmsDatas[iIndex].Subject + "</td>\r\n" +
                        "   <td><a href='" + Url + "' target='_blank'>" + CmsDatas[iIndex].DataAlias + "</a><br>" + CmsDatas[iIndex].Subject + "</td>\r\n" +
                        "   <td>" + CmsDatas[iIndex].ReleaseDate + "</td>\r\n" +
                        "   <td>" + CmsDatas[iIndex].EffectedDate + "</td>\r\n" +
                        "</tr>\r\n";

                }
                Html += "</table>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
